/*******************************************************************************
 *
 * File Name:          SystemContext.java
 *
 * Created:            2006-10-3 8:45:37
 *
 * Original Author:    yu.peng
 *
 *------------------------------------------------------------------------------
 * MODIFICATION HISTORY
 *------------------------------------------------------------------------------
 * WHEN                WHO                 WHY
 * 2006-10-3 8:45:37 yu.peng             new
 *------------------------------------------------------------------------------
 *
 ******************************************************************************/
/*******************/
/* PACKAGE SECTION */
/*******************/
package org.njo.webapp.root.utility;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.njo.webapp.root.model.dao.AppPropertiesDAO;

/**
 * TODO:comment
 *
 * @author yu.peng
 * @version 0.01
 */
public final class SystemContext {

    public static Log logger = LogFactory.getLog(SystemContext.class);

    private static Properties properties = null;
    
    private SystemContext() {
    }

    /**
     * 取得数据库连接.
     *
     * 需要使用容器提供的数据库连接.
     * @return Connection 数据库连接
     */
    public static Connection openConnection() throws SQLException {
        Connection connection = null;
        try {
            Context context = new InitialContext();
            DataSource ds = (DataSource) context.lookup("java:comp/env/jdbc/webdb");
            connection = ds.getConnection();
            connection.setAutoCommit(false);
        } catch (NamingException e) {
            if (logger.isErrorEnabled()) {
                logger.error(e);
            }
            throw new SQLException(e.getMessage());
        } catch (SQLException e) {
            if (logger.isErrorEnabled()) {
                logger.error(e);
            }
            throw e;
        }
        return connection;
    }

    /**
     * 关闭数据库连接.
     * 释放数据库连接.
     * 
     * @param connection
     */
    public static void closeConnection(Connection connection) throws SQLException {
        try {
            connection.close();
        } catch (SQLException e) {
            if (logger.isErrorEnabled()) {
                logger.error(e);
            }
            throw e;
        }
    }

    /**
     * 从数据库读取属性.
     * 
     * TODO 从数据库中取得属性值,如果要是写成struts的plug-in,效果可能更好
     */
    private static void initProperties() {
        if (properties == null) {
            properties = new Properties();
            Connection connection = null;
            try {
                connection = SystemContext.openConnection();
                AppPropertiesDAO appPropertiesDAO = new AppPropertiesDAO(
                        connection);
                List propList = appPropertiesDAO.selectAllRecords(1,
                        Integer.MAX_VALUE);
                for (int i = 0; i < propList.size(); i++) {
                    String[] prop = (String[]) propList.get(i);
                    properties.setProperty(prop[0], prop[2]);
                }
            } catch (SQLException e) {
                if (logger.isErrorEnabled()) {
                    logger.error("loadMessages SQLException.", e);
                }
            } finally {
                try {
                    if (connection != null) {
                        SystemContext.closeConnection(connection);
                    }
                } catch (Throwable tw) {
                }
            }
        }
    }
    
    /**
     * 取得配置文件中的参数.
     */
    public static String getProperty(String key) {
        return getProperty(key, null);
    }

    /**
     * 取得配置文件中的参数.
     */
    public static String getProperty(String key, String dufaultValue) {
        if (properties == null) {
            initProperties();
        }
        return properties.getProperty(key, dufaultValue);
    }

    /**
     * 取得配置文件中的参数.
     * @param key
     */
    public static int getPropertyAsInt(String key) {
        return getPropertyAsInt(key, 0);
    }

    /**
     * 取得配置文件中的参数.
     * @param key
     */
    public static int getPropertyAsInt(String key, int defaultValue) {
        int value = 0;
        try {
            value = Integer.parseInt(getProperty(key));
        } catch (Exception e) {
            value = defaultValue;
        }
        return value;
    }
    
    /**
     * 重置参数.
     */
    public static void resetProperties() {
        properties = null;
    }
    
}
